package com.team.employment.web.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.employment.common.exception.CustomException;
import com.team.employment.common.result.ResultCodeEnum;
import com.team.employment.model.entity.*;
import com.team.employment.model.enums.BaseStatus;
import com.team.employment.model.enums.UserType;
import com.team.employment.web.admin.dto.CompanyQueryDto;
import com.team.employment.web.admin.dto.CompanyStatutoryDto;
import com.team.employment.web.admin.mapper.*;
import com.team.employment.web.admin.service.CompanyInfoService;

import com.team.employment.web.admin.service.StatutoryInfoService;
import com.team.employment.web.admin.service.UserInfoService;
import com.team.employment.web.admin.vo.BenefitBriefVo;
import com.team.employment.web.admin.vo.CompanyDetailVo;
import com.team.employment.web.admin.vo.CompanyStatutoryVo;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 陈钰儿
* @description 针对表【company_info(公司信息表)】的数据库操作Service实现
* @createDate 2025-04-11 15:49:26
*/
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo>
    implements CompanyInfoService{

    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    @Autowired
    private CompanyBenefitsMapper companyBenefitsMapper;

    @Autowired
    private BenefitInfoMapper benefitInfoMapper;

    @Autowired
    private RecruitmentInfoMapper recruitmentInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private CompanyStatutoryMapper companyStatutoryMapper;
    @Autowired
    private StatutoryInfoMapper statutoryInfoMapper;
    @Autowired
    private ResumeInfoMapper resumeInfoMapper;

    @Override
    public IPage<CompanyStatutoryVo> pageInfo(IPage<CompanyStatutoryVo> page) {
        IPage<CompanyStatutoryVo> list = companyInfoMapper.pageInfo(page);
       return list;
    }

    @Override
    public IPage<CompanyStatutoryVo> pageQueryInfo(Page<CompanyStatutoryVo> page, CompanyQueryDto companyQueryDto) {
        IPage<CompanyStatutoryVo> list = companyInfoMapper.pageQueryInfo(page,companyQueryDto);
        return list;
    }

    @Override
    @Transactional
    public CompanyDetailVo getInfoById(Long id) {
        CompanyInfo companyInfo = companyInfoMapper.selectById(id);

        LambdaQueryWrapper<CompanyBenefits> companyBenefitsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyBenefitsLambdaQueryWrapper.eq(CompanyBenefits::getCompanyId,id);
        List<CompanyBenefits> companyBenefits = companyBenefitsMapper.selectList(companyBenefitsLambdaQueryWrapper);

        LambdaQueryWrapper<BenefitInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Long> benefitIds = companyBenefits.stream().map(CompanyBenefits::getBenefitsId).collect(Collectors.toList());
        if(!benefitIds.isEmpty()){
            lambdaQueryWrapper.in(BenefitInfo::getId,benefitIds).eq(BenefitInfo::getStatus,BaseStatus.ENABLE);
        }
        List<BenefitInfo> benefitInfos = benefitInfoMapper.selectList(lambdaQueryWrapper);

        ArrayList<BenefitBriefVo> benefitBriefVos = new ArrayList<>();
        benefitInfos.forEach(benefitInfo -> {
            BenefitBriefVo briefVo = BenefitBriefVo.builder().benefitId(benefitInfo.getId())
                    .benefitName(benefitInfo.getName())
                    .benefitUrl(benefitInfo.getIcon())
                    .build();
            benefitBriefVos.add(briefVo);
        });

        CompanyDetailVo companyDetailVo = new CompanyDetailVo();
        BeanUtils.copyProperties(companyInfo,companyDetailVo);
        companyDetailVo.setBenefitBriefVOList(benefitBriefVos);

        return companyDetailVo;
    }

    @Override
    @Transactional
    public void removeInfoById(Long id) {
        //1、删除企业信息
        companyInfoMapper.deleteById(id);

        //2、删除招聘信息
        LambdaQueryWrapper<RecruitmentInfo> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(RecruitmentInfo::getCompanyId,id);
        recruitmentInfoMapper.delete(lambdaQueryWrapper1);

        //3、删除法定人&公司关联表信息
        LambdaQueryWrapper<CompanyStatutory> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(CompanyStatutory::getCompanyId,id);
        companyStatutoryMapper.delete(lambdaQueryWrapper2);
    }

    @Override
    @Transactional
    public void registerCompany(CompanyStatutoryDto companyStatutoryDto,String phone) {

        //根据手机号查询已注册的企业者用户
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getPhone,phone)
                .eq(UserInfo::getType,UserType.ENTREPRENEURS.getCode()));
        if (userInfo == null){
            throw new CustomException(ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);//没有找到对应的企业者用户
        }

        //设置公司信息
        CompanyInfo companyInfo = companyStatutoryDto.getCompanyInfo();
        companyInfo.setId(userInfo.getId());//使用用户（企业者）ID作为公司ID
        companyInfoMapper.insert(companyInfo);
        //设置法定人信息
        StatutoryInfo statutoryInfo = companyStatutoryDto.getStatutoryInfo();
        statutoryInfoMapper.insert(statutoryInfo);
        //保存企业与法定人的关联表
        CompanyStatutory rel = new CompanyStatutory();
        rel.setCompanyId(companyInfo.getId());
        rel.setStatutoryId(statutoryInfo.getId());
        companyStatutoryMapper.insert(rel);

    }

    @Override
    @Transactional
    public void updateCompany(CompanyStatutoryDto dto){
        //更新公司信息
        companyInfoMapper.updateCompany(dto.getCompanyInfo());
        // 更新法定代表人信息
        statutoryInfoMapper.updateStatutory(dto.getStatutoryInfo());

    }

    @Override
    public IPage<ResumeInfo> getJobApplication(Long id, int pageNum, int pageSize) {
        Page<ResumeInfo> page = new Page<>(pageNum, pageSize);
        return resumeInfoMapper.findByCompanyId(page,id);
    }

    @Override
    public CompanyInfo getCompanyInfoByRecruitmentId(Long id) {
        RecruitmentInfo recruitmentInfo = recruitmentInfoMapper.selectById(id);
        LambdaQueryWrapper<CompanyInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyInfo::getId,recruitmentInfo.getCompanyId());
        CompanyInfo companyInfo = companyInfoMapper.selectOne(queryWrapper);
        return companyInfo;

    }

}




